package com.sengmitnick.timelogger;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;

public class ServiceAbility extends Ability {
    // 定义日志标签
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "MY_TAG");
    private static final int NOTIFICATION_ID = 0XD0000002;

    private ServiceRemoteObject remote = new ServiceRemoteObject();

    @Override
    public void onStart(Intent intent) {
        startForeground();
        super.onStart(intent);
        HiLog.info(LABEL, "ServiceAbility::onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelBackgroundRunning();
        HiLog.info(LABEL, "ServiceAbility::onStop");
    }

    // FA在请求PA服务时会调用Ability.connectAbility连接PA，连接成功后，需要在onConnect返回一个remote对象，供FA向PA发送消息
    @Override
    protected IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return remote.asObject();
    }

    private void startForeground() {
        NotificationRequest request = new NotificationRequest(NOTIFICATION_ID).setTapDismissed(true);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("Time Logger").setText("Time Logger 正在后台运行").setAdditionalText("");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
            content);
        request.setContent(notificationContent);
        keepBackgroundRunning(NOTIFICATION_ID, request);
    }

    static class ServiceRemoteObject extends RemoteObject implements IRemoteBroker {
        private static final int SUCCESS = 0;
        private static final int ERROR = 1;
        private static final int PLUS = 1001;
        private static final int GETAUTHORIZATION = 1002;
        private static final int SETAUTHORIZATION = 1003;

        ServiceRemoteObject() {
            super("MyService_MyRemote");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            HiLog.info(LABEL, "ServiceAbility::onRemoteRequest");
            switch (code) {
                case PLUS: {
                    String dataStr = data.readString();
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put("code", SUCCESS);
                    Service.plus(dataStr);
                    reply.writeString(ZSONObject.toZSONString(result));
                    break;
                }
                case GETAUTHORIZATION: {
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put("code", SUCCESS);
                    result.put("data", Service.getAuthorization());
                    reply.writeString(ZSONObject.toZSONString(result));
                    break;
                }
                case SETAUTHORIZATION: {
                    String dataStr = data.readString();
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put("code", SUCCESS);
                    result.put("data", dataStr);
                    Service.setAuthorization(dataStr);
                    reply.writeString(ZSONObject.toZSONString(result));
                    break;
                }
                default: {
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put("code", ERROR);
                    reply.writeString(ZSONObject.toZSONString(result));
                    return false;
                }
            }
            return true;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }

    }
}